﻿@model apiApplication1.Controllers.Product

@{
    ViewBag.Title = "Register";
}
@using (Html.BeginForm())
{
    <fieldset>
        <legend>添加商品</legend>
        <div class="editor-label">
            名称：
        </div>
        <div class="editor-field">
            <input data-bind='value: name' />
        </div>

        <div class="editor-label">
            价格：
        </div>
        <div class="editor-field">
            <input data-bind='value: price' />
        </div>

        <div class="editor-label">
            分类：
        </div>
        <div class="editor-field">
            <input data-bind='value: CategoryId' />
        </div>

        <div class="editor-label">
            Email：
        </div>
        <div class="editor-field">
            <input data-bind='value: Email' />
        </div>
        <p>
            <input type="button" value="Create" data-bind="click:Register" />
        </p>
    </fieldset>
}
<style type="text/css">
    .validationMessage {
        color: red;
        padding-left: 10px;
    }
</style>
<script type="text/ecmascript">
    var Product = function () {
        var self = this;

        self.CategoryId = ko.observable().extend({
            required: true
        });

        self.price = ko.observable().extend({
            required: { params: true, message: "请输入价格" },
            min: { params: 1, message: "请输入大于1的整数" },
            max: 100
        });

        self.name = ko.observable().extend({
            minLength: 2,
            maxLength: { params: 30, message: "名称最大长度为30个字符" },
            required: {
                params: true,
                message: "请输入名称",
            }
        });

        self.Email = ko.observable().extend({
            required: {
                params: true,
                message: "Please enter your email"
            },
            email: {
                params: true,
                message: "The format is not correct"
            }
        });



        self.Register = function () {
            self.errors = ko.validation.group(self);
            if (self.isValid()) {
                alert('data sent');
            } else {
                self.errors.showAllMessages();
            }
        };



    }
    var viewModel = new Product();
    ko.applyBindings(viewModel);

</script>
